import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { productService, Comment, Product } from '../shared/product.service';

@Component({
  selector: 'app-product-detail',
  templateUrl: './product-detail.component.html',
  styleUrls: ['./product-detail.component.css']
})
export class ProductDetailComponent implements OnInit {

  product:Product;//产品对象
  productID:number;//产品对应的id
  comments:Comment[];//产品对应的评论数组
  newRating:number = 5;//新的排名计数
  newComment:string = '';//新的评论说明
  isAddComment:boolean = false;//控制页面的显示隐藏处理

  constructor(public routeinfo:ActivatedRoute,public productservice:productService) { }

  ngOnInit() {
    this.productID = this.routeinfo.snapshot.params['id'];
    //订阅产品对应的数据
    this.productservice.getProduct(this.productID).subscribe(product => this.product = product[0]);
    //订阅当前的产品对应的品论信息
    this.productservice.getCommentForProduct(this.productID).subscribe(conments => this.comments = conments);
  }

  /**
   * @description 函数在页面上新增新的评论说明
   */
  addComment(){
    let _commnet:Comment = new Comment(0,this.productID,'11256358','lihua',this.newRating,this.newComment);
    this.comments.unshift(_commnet);
    //在提交了品论的数据之后初始评价星级为5星，设置评论的初始化为空
    this.newComment = '';
    this.newRating = 5;
    this.isAddComment = false;
  }

}
